/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.entity.product;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;

/**
 * 商品分类Entity
 * @author Jackie.Liu
 * @version 2014-10-28
 */
@Entity
@Table(name = "ivm_product_classify")
@DynamicInsert @DynamicUpdate
public class ProductClassify extends IdEntity<ProductClassify> {

	private static final long serialVersionUID = 1L;
	private String level;
	private ProductClassify parent;
	private String name; 	// 名称
	private Merchant merchant; //商户信息
	private String shopUrl; //分类外挂的电商链接
	private Integer sort; //排序号
	
	private List<ProductClassify> childList = Lists.newArrayList();	// 拥有子分类列表

	public ProductClassify() {
		super();
		this.sort = 30;
		this.level = "1";
	}

	public ProductClassify(String id){
		this();
		this.id = id;
	}
	
	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent_id")
	@NotFound(action = NotFoundAction.IGNORE)
	public ProductClassify getParent() {
		return parent;
	}

	public void setParent(ProductClassify parent) {
		this.parent = parent;
	}

	@Length(min=1, max=200)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne
	@JoinColumn(name="merchant_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message="所属商户信息不能为空")
	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public String getShopUrl() {
		return shopUrl;
	}

	public void setShopUrl(String shopUrl) {
		this.shopUrl = shopUrl;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	@OneToMany(mappedBy = "parent", fetch=FetchType.LAZY)
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy(value="sort") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<ProductClassify> getChildList() {
		return childList;
	}

	public void setChildList(List<ProductClassify> childList) {
		this.childList = childList;
	}

	@Transient
	public static void sortList(List<ProductClassify> list, List<ProductClassify> sourcelist){
		for (int i=0; i<sourcelist.size(); i++){
			ProductClassify e = sourcelist.get(i);
			if (e.getParent()==null || e.getParent().getId()==null){
				//添加上级分类
				list.add(e);
				// 判断是否还有子节点, 有则继续获取子节点
				for (int j=0; j<sourcelist.size(); j++){
					ProductClassify childe = sourcelist.get(j);
					if (childe.getParent()!=null && childe.getParent().getId()!=null
							&& childe.getParent().getId().equals(e.getId())){
						//添加子级分类
						list.add(childe);
					}
				}
			}
		}
	}
	
}


